package persistence.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import model.User;

public class UserDAOPool implements UserDAO {

    private static UserDAOPool persistenceManager = null;
    private DataSource pool;
    private static final Logger logger = Logger.getLogger(UseDAOJDBC.class.getName());

    private UserDAOPool() {
    }

    public static UserDAOPool getUserDAOPoolImplementation() {
        if (persistenceManager == null) {
            persistenceManager = new UserDAOPool();
        }
        return persistenceManager;
    }

    @Override
    public boolean setUp(String url, String driver, String user, String password) {
        Context env = null;
        try {
            env = (Context) new InitialContext().lookup("java:comp/env");
            pool = (DataSource) env.lookup("AppbuyPool");
            if (pool == null) {
                logger.log(Level.SEVERE, "No se encontro el DataSource");
                return false;
            }
        } catch (NamingException ex) {
            logger.log(Level.SEVERE, "No se pudo abrir la conexi�n contra base de datos", ex);
            return false;
        }

        return true;
    }

    @Override
    public boolean createUser(User user) {
        UserDAO jDBCUserDAO = prepareForExecutingQuerry();
        if (jDBCUserDAO == null) {
            return false;
        }
        boolean isExecutedOk = jDBCUserDAO.createUser(user);
        releaseQuerryResources(jDBCUserDAO);
        return isExecutedOk;
    }

    @Override
    public boolean deleteUser(String name) {
        UserDAO jDBCUserDAO = prepareForExecutingQuerry();
        if (jDBCUserDAO == null) {
            return false;
        }
        boolean isExecutedOk = jDBCUserDAO.deleteUser(name);
        releaseQuerryResources(jDBCUserDAO);
        return isExecutedOk;
    }

    @Override
    public User confirmUser(String name, String password) {
        UserDAO jDBCUserDAO = prepareForExecutingQuerry();
        if (jDBCUserDAO == null) {
            return null;
        }
        User user = jDBCUserDAO.confirmUser(name, password);
        releaseQuerryResources(jDBCUserDAO);
        return user;
    }

    @Override
    public User readUser(String name) {
        UserDAO jDBCUSerDAO = prepareForExecutingQuerry();
        if (jDBCUSerDAO == null) {
            return null;
        }
        User user = jDBCUSerDAO.readUser(name);
        releaseQuerryResources(jDBCUSerDAO);
        return user;
    }

    @Override
    public boolean updateUser(User user) {
        UserDAO jDBCUserDAO = prepareForExecutingQuerry();
        if (jDBCUserDAO == null) {
            return false;
        }
        boolean isExecutedOk = jDBCUserDAO.updateUser(user);
        releaseQuerryResources(jDBCUserDAO);
        return isExecutedOk;
    }

    @Override
    public boolean disconnect() {
        return true;
    }

    private UserDAO prepareForExecutingQuerry() {
        UseDAOJDBC jDBCpersistenceManager = new UseDAOJDBC();
        Connection connection;
        try {
            connection = pool.getConnection();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "No se pudo abrir la conexion contra la base de datos", ex);
            return null;
        }
        jDBCpersistenceManager.setConnection(connection);
        return jDBCpersistenceManager;
    }

    private void releaseQuerryResources(UserDAO userDAO) {
        userDAO.disconnect();
    }
}
